<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>
<!-- 3. https://www.jq22.com/yanshi21648 穿梭框 -->

<body>
    <div id="app">
        <div class="list-container" style=" display: flex;">
            <div class="left" style="width: 200px; height: 300px; border: 1px solid #ccc; overflow-y: auto">
                <h2>左侧列表</h2>
                <ul class="list">
                    <li v-for="item in leftList" :key="item">
                        <input type="checkbox" v-model="isSelected(item, 'left')"
                            @click="toggleSelection(item, 'left')">
                        {{ item }}
                    </li>
                </ul>
            </div>
            <div>
                <button @click="moveSelectedItems" style="margin: 0 30px; margin-top: 100%;">《》</button>
            </div>
            <div class="right" style="width: 200px; height: 300px; border: 1px solid #ccc; overflow-y: auto">
                <h2>右侧列表</h2>
                <ul class="list">
                    <li v-for="item in rightList" :key="item">
                        <input type="checkbox" v-model="isSelected(item, 'right')"
                            @click="toggleSelection(item, 'right')">
                        {{ item }}
                    </li>
                </ul>
            </div>
        </div>
    </div>

</body>
<script>
    const { createApp, ref } = Vue;
    const app = createApp({
        setup() {
            const leftList = ref(['张三', '李四', '王五', '赵六', '张龙', '赵虎', '王朝', '马汉']);
            const rightList = ref([]);
            const selected = ref({ left: [], right: [] });

            function isSelected(item, list) {
                return selected.value[list].includes(item);
            }

            function toggleSelection(item, list) {
                const index = selected.value[list].indexOf(item);
                if (index > -1) {
                    selected.value[list].splice(index, 1);
                } else {
                    selected.value[list].push(item);
                }
            }

            function moveSelectedItems() {
                if (selected.value.left.length > 0) {
                    rightList.value = rightList.value.concat(selected.value.left);
                    leftList.value = leftList.value.filter(item => !selected.value.left.includes(item));
                    selected.value.left = [];
                } else if (selected.value.right.length > 0) {
                    leftList.value = leftList.value.concat(selected.value.right);
                    rightList.value = rightList.value.filter(item => !selected.value.right.includes(item));
                    selected.value.right = [];
                }
            }
            return {
                leftList,
                rightList,
                isSelected,
                toggleSelection,
                moveSelectedItems
            }
        }
    }).mount('#app');
</script>

</html>